<?php
namespace app\api\controller;
use BaiduAi\AipOcr;
use think\Db;
use think\Controller;
use think\Request;

class Index extends Controller
{
    private $imageRoot;
    private $app_id='';//百度APPID
    private $api_key='';//百度API_KEY
    private $secret_key='';//百度SECRET_KEY
    public function _initialize(){
        $this->imageRoot=Reqeust::instance->domain().'uploads/';
    }

    /**
     * 图片上传接口
     * @return string
     * @author liuming
     **/
    function upload($file)
    {
      $file = request()->file('file');
    
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            
            $image= $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
           $image=$this->imageRoot.$image;
          
          return $image;
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }   
    }
    /**
     * 通用图片文字识别
     * @return array
     * @author 
     **/
    function basicGeneral($file)
    {
          $iamge=$this->upload($file);
          $client=new AipOcr($this->app_id,$this->api_key,$this->app_secret);
          $data=$client->basicGeneralUrl($image);
          return $data;
          
    }

}
